package com.company.finance.ddd.domain.core.bill.aggregate;

import com.company.finance.ddd.common.IRepository;
import com.company.finance.ddd.domain.core.voucher.VoucherService;
import com.company.finance.ddd.domain.core.bill.enums.BillSubTypeEnum;
import lombok.Builder;
import lombok.NonNull;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * bill的SUB_TYPE_CODE与Repository的映射关系见{@link BillSubTypeMapper}
 * @author wanglei
 */
@Builder
class Items {

    @NonNull
    private final Bill bill;
    @NonNull
    private final VoucherService voucherService;

    List<?> getItems() {
        return ((IRepository<?>)voucherService.getApplicationContext()
                .getBean(BillSubTypeMapper.getClassPrefixName(BillSubTypeEnum.getBillSubTypeEnum(bill.getSUB_TYPE_CODE())) + "ItemRepository"))
                .query(buildQueryParam());
    }

    private Map<String, Object> buildQueryParam() {
        Map<String, Object> param = new HashMap<>(1);
        param.put("ID", bill.getID());
        return param;
    }
}
